﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class CADMAPINTEXData : BaseFunction
    {
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\CADMAPINTEX.xls");
        public CADMAPINTEXData() 
        {
           
        } 
        public DataSet ReSetData()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent();
                }
                #region=======MAPINTEX================
                string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface");
                string[] files = System.IO.Directory.GetFiles(fileroot);
                //string filepath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\CADMAPINTEX.xls");
                string ShortName = "";

                foreach (string file in files)
                {
                    if (!file.EndsWith("MAPINTEX.xls"))
                        continue;
                    if (System.IO.File.Exists(file))
                    {
                        ShortName = file.Substring(file.LastIndexOf("\\") + 1);
                        setResult = new DataSet();
                        setResult = exAgent.LoadDataTableDef(file, "");
                        string xml = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\" + ShortName.Replace(".xls", ".XML"));
                        setResult.DataSetName = ShortName.Replace(".xls", "");
                        if (System.IO.File.Exists(xml))
                        {
                            System.IO.File.Delete(xml);
                        }
                        if (setResult != null)
                        {
                            setResult.WriteXml(xml);
                        }
                    }
                }
                #endregion
                #region=======PRODSETUP================
                foreach (string file in files)
                {
                    if (!file.EndsWith("PRODSETUP.xls"))
                        continue;
                    setResult = exAgent.LoadDataTableDef(file, "");
                    ShortName = file.Substring(file.LastIndexOf("\\") + 1);
                    string xml = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\" + ShortName.Replace(".xls", ".XML"));
                    if (System.IO.File.Exists(xml))
                    {
                        System.IO.File.Delete(xml);
                    }
                    setResult.WriteXml(xml);
                #endregion
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.KillExcelProcess();
                if (exAgent != null)
                    exAgent.Close();
            }
            return setReturn;
        }
        public DataSet LoadSetCADMAPINTEX(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filepath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\CADMAPINTEX.xml");
                setResult = new DataSet();
                if (System.IO.File.Exists(filepath))
                {
                    setResult.ReadXml(filepath);
                    setReturn.Merge(setResult);
                }
                filepath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\FINISHMAP.xml");
                setResult = new DataSet();
                if (System.IO.File.Exists(filepath))
                {
                    setResult.ReadXml(filepath);
                    setReturn.Merge(setResult);
                }
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}